package ru.pashkan.spring.db.service.jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.pashkan.spring.db.dao.UserDao;
import ru.pashkan.spring.db.service.UserService;
import ru.pashkan.spring.entity.User;

import java.util.Collection;
import java.util.List;

@Component
public class UserServiceJdbc implements UserService {

    @Autowired
    private UserDao userDao;

//    public UserDao getUserDao() {
//        return userDao;
//    }
//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//    }

    @Transactional
    @Override
    public Boolean add(User user) {

        List<User> duplicate = userDao.findUser(user.getLogin(), "mail");

        if (duplicate.isEmpty()) {

            userDao.insert(user);

            return true;
        }
        return false;
    }

    @Override
    public void update(User user) {

//        int i = userDao.getMaxUserId();
//        System.out.println("+++++++++++++++++++++++++ " + i);
//        i++;
//        user.setUserId(i);
        userDao.update(user);
    }

    @Override
    public Collection<User> getUser(String search) {
        return null;
    }

    @Override
    public void delete(User user) {
        userDao.delete(user);
    }

    @Override
    public void delete(int id) {
        userDao.delete(id);
    }

    //-------------------------------
    @Override
    public List<User> getAllUsers() {
        return userDao.getAll();
    }

}
